import Router from 'koa-router';
import axios from './utils/axios'
const router = new Router({prefix: '/cart'})
import Cart from '../dbs/models/cart'
import md5 from 'crypto-js/md5'

// 创建购物车
router.post('/create',async ctx => {
  // ctx.request.body post传参
  const {params} = ctx.request.body
  console.log(params);
  let time = Date.now()
  let cart_id = md5(Math.random() * 1000 + time).toString()
  const res = await Cart.insert({
    id: params.id,
    detail: params.detail,
    user: ctx.session.passport.user,
    add_time: time,
    cart_id: cart_id
  })
  if(res) {
    ctx.body = {
      code: 0,
      id: cart_id
    }
  }else {
    ctx.body = {
      code: -1
    }
  }
  
})

router.get('/getCart',async ctx => {
  // ctx.request.body post传参
  const {id} = ctx.query  
  // console.log(params);
  const res = await Cart.find({id})
  ctx.body = {
    code: 0
  }
})



export default router